home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10259 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  109 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ?? How to dump text files to screen ??
  5. Date: 15 Mar 1996 22:24:34 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4idmr2INNol@keats.ugrad.cs.ubc.ca>
  8. References: <31430CE8.469E@aol2.com> <4iddva$aic@sue.cc.uregina.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4iddva$aic@sue.cc.uregina.ca>,
  12. Tristan Psionic <tristan@nether.net> wrote:
  13. >In <31430CE8.469E@aol2.com>, Neil <neil@aol2.com> writes:
  14. >>I need help -- how can I open up a text file and just dump it
  15. >>all out (in plain text) to the screen?
  16. >Well, you can't do exactly that, but this is pretty close (and pretty raw):
  17. >
  18. >#include <stdio.h>
  19. >
  20. >void main (int argc,char argv[])
  21. >{
  22. >  FILE *fp;
  23. >  char s[1000];
  24. >  if (argc >= 1)
  25. >  {
  26. >    fp=fopen(argv[1],"r");
  27. >    while (gets(s,1000,fp) != NULL)
  28. >    {
  29. >      printf("%s",s);
  30. >    }
  31. >    fclose(fp);
  32. >  }
  33. >  else
  34. >  {
  35. >    printf("usage:  dumptext [filename]"
  36. >  }
  37. >}
  38. >
  39. >That should work.  I'm not an expert at C or anything, but I do believe that
  40. >there aren't any hugely flawed errors in it.  Good luck.
  41.  
  42. Good luck with correctly reproducing lines over 1000 characters.
  43.  
  44. Wrong return value from main(). Should be int. 
  45.  
  46. No exit status. Unbalanced parenthesis in printf().
  47.  
  48. Tries to use argv[1] even if argc is merely equal to 1.
  49.  
  50. Never checks whether fopen() returns NULL.
  51.  
  52. A NULL return from fgets() could indicate an end of file _or_ an error.
  53.  
  54. gets() is written mistakenly instead of fgets().
  55.  
  56.  
  57. No HUGELY flawed errors, though. Nope. :)
  58.  
  59. #include <stdio.h>
  60. #include <errno.h>
  61. #include <stdlib.h>
  62.  
  63. /*
  64.  * a useless form of 'cat'
  65.  */
  66.  
  67. int main(int argc, char *argv[])
  68.  
  69. {
  70.     FILE *input;
  71.     char **arg, c;
  72.     
  73.     if (argc > 1) {        /* file arguments available    */
  74.         for (arg = argv + 1; arg < argv + argc; arg++) {
  75.             input = fopen(*arg,"r");
  76.             if (!input) {
  77.                 perror(*arg);
  78.                 exit(EXIT_FAILURE);
  79.             }
  80.             while ((c = getc(input)) != EOF)
  81.                 if (putc(c, stdout) == EOF)
  82.                     break;
  83.             if (ferror(input)) {
  84.                 perror(*arg);
  85.                 exit(EXIT_FAILURE);
  86.             }
  87.             if (ferror(stdout)) {
  88.                 perror("standard output");
  89.                 exit(EXIT_FAILURE);
  90.             }
  91.             fclose(input);
  92.         }
  93.     } else {        /* no arguments given        */
  94.         fprintf(stderr,"usage: %s [ file1 ] [ file2 ] ...\n",
  95.             argv[0] ? argv[0] : "<command_name>");
  96.     }
  97.     return 0;
  98. }
  99.  
  100. I actually tested that, unlike you, with all the diagnostics the compiler can
  101. muster enabled. No room for cockiness on comp.lang.c.
  102.  
  103. There is no need to use a buffer. The putc() and getc() functions are often
  104. defined as macros which operate directly and efficiently on the FILE structure,
  105. calling for buffer flushes and replenishes as needed. The while() loop above is
  106. probably more efficient than fgets(), and doesn't require extra buffering.
  107. -- 
  108.  
  109.